//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "SteepestDescent.h"
using namespace Cephei::QL::Math::Optimization;
#include <gen/QL/Math/Optimization/LineSearch.h>
#include <gen/QL/Math/Optimization/Problem.h>
#include <gen/QL/Math/Optimization/EndCriteria.h>
#include <gen/QL/Math/Optimization/LineSearchBasedMethod.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Optimization::CSteepestDescent::CSteepestDescent (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::ILineSearch^>^ lineSearch) : CLineSearchBasedMethod(CSteepestDescent::typeid)
{
    CLineSearch^ _ClineSearch;
    try
    {
#ifdef HANDLE
        _phSteepestDescent = NULL;
#endif
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::ILineSearch^>::IsSome::get (lineSearch))
        {
            _ClineSearch = safe_cast<CLineSearch^> (lineSearch->Value);
            _ClineSearch->Lock();
        }
        boost::shared_ptr<QuantLib::LineSearch>& _lineSearch = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::ILineSearch^>::IsSome::get (lineSearch) ? static_cast<boost::shared_ptr<QuantLib::LineSearch>&> (_ClineSearch->GetShared ()) : boost::shared_ptr<QuantLib::LineSearch>()); //1
        _ppSteepestDescent = new boost::shared_ptr<QuantLib::SteepestDescent> (new QuantLib::SteepestDescent ( _lineSearch ));
        SetLineSearchBasedMethod (boost::dynamic_pointer_cast<QuantLib::LineSearchBasedMethod> (*_ppSteepestDescent));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_ClineSearch != nullptr) _ClineSearch->Unlock();
    }
}
Cephei::QL::Math::Optimization::CSteepestDescent::CSteepestDescent (boost::shared_ptr<QuantLib::SteepestDescent>& childNative, Object^ owner) : CLineSearchBasedMethod(CSteepestDescent::typeid)
{
#ifdef HANDLE
	_phSteepestDescent = NULL;
#endif
	_ppSteepestDescent = &childNative;
    _ppLineSearchBasedMethod = new boost::shared_ptr<QuantLib::LineSearchBasedMethod> (boost::dynamic_pointer_cast<QuantLib::LineSearchBasedMethod> (*_ppSteepestDescent));
}
Cephei::QL::Math::Optimization::CSteepestDescent::CSteepestDescent (QuantLib::SteepestDescent& childNative, Object^ owner) : CLineSearchBasedMethod(CSteepestDescent::typeid)
{
#ifdef HANDLE
	_phSteepestDescent = NULL;
#endif
	_ppSteepestDescent = new boost::shared_ptr<QuantLib::SteepestDescent> (&childNative);
    _ppLineSearchBasedMethod = new boost::shared_ptr<QuantLib::LineSearchBasedMethod> (boost::dynamic_pointer_cast<QuantLib::LineSearchBasedMethod> (*_ppSteepestDescent));
    _SteepestDescentOwner = owner;
    _LineSearchBasedMethodOwner = owner;
}

Cephei::QL::Math::Optimization::CSteepestDescent::CSteepestDescent (CSteepestDescent^ copy) : CLineSearchBasedMethod(CSteepestDescent::typeid)
{
#ifdef HANDLE
	_phSteepestDescent = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSteepestDescent = new boost::shared_ptr<QuantLib::SteepestDescent> (copy->GetShared());
        _ppLineSearchBasedMethod = new boost::shared_ptr<QuantLib::LineSearchBasedMethod> (boost::dynamic_pointer_cast<QuantLib::LineSearchBasedMethod> (*_ppSteepestDescent));
    }
}
Cephei::QL::Math::Optimization::CSteepestDescent::CSteepestDescent (System::Type^ t) : CLineSearchBasedMethod(CSteepestDescent::typeid)
{
#ifdef HANDLE
	_phSteepestDescent = NULL;
#endif
	if (!t->IsSubclassOf(CSteepestDescent::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Optimization::CSteepestDescent::CSteepestDescent (QuantLib::Handle<QuantLib::SteepestDescent>& childNative, Object^ owner)  : CLineSearchBasedMethod(CSteepestDescent::typeid)
{
	_phSteepestDescent = &childNative;
	_ppSteepestDescent = &static_cast<boost::shared_ptr<QuantLib::SteepestDescent>>(childNative.currentLink());
    _ppLineSearchBasedMethod = new boost::shared_ptr<QuantLib::LineSearchBasedMethod> (boost::dynamic_pointer_cast<QuantLib::LineSearchBasedMethod> (*_ppSteepestDescent));
    _SteepestDescentOwner = owner;
}
Cephei::QL::Math::Optimization::CSteepestDescent::CSteepestDescent (QuantLib::Handle<QuantLib::SteepestDescent> childNative)  : CLineSearchBasedMethod(CSteepestDescent::typeid)
{
	_phSteepestDescent = &childNative;
	_ppSteepestDescent = &static_cast<boost::shared_ptr<QuantLib::SteepestDescent>>(childNative.currentLink());
    _ppLineSearchBasedMethod = new boost::shared_ptr<QuantLib::LineSearchBasedMethod> (boost::dynamic_pointer_cast<QuantLib::LineSearchBasedMethod> (*_ppSteepestDescent));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Optimization::CSteepestDescent::CSteepestDescent (QuantLib::SteepestDescent childNative)  : CLineSearchBasedMethod(CSteepestDescent::typeid)
{
#ifdef HANDLE
	_phSteepestDescent = NULL;
#endif
	_ppSteepestDescent = new boost::shared_ptr<QuantLib::SteepestDescent> (new QuantLib::SteepestDescent (childNative));
    _ppLineSearchBasedMethod = new boost::shared_ptr<QuantLib::LineSearchBasedMethod> (boost::dynamic_pointer_cast<QuantLib::LineSearchBasedMethod> (*_ppSteepestDescent));
}
#endif

Cephei::QL::Math::Optimization::CSteepestDescent::~CSteepestDescent ()
{
    if (_ppSteepestDescent != NULL)
    {
	    delete _ppSteepestDescent;
        _ppSteepestDescent = NULL;
    }
}
Cephei::QL::Math::Optimization::CSteepestDescent::!CSteepestDescent ()
{
    if (_ppSteepestDescent != NULL)
    {
	    delete _ppSteepestDescent;
    }
}
QuantLib::SteepestDescent& Cephei::QL::Math::Optimization::CSteepestDescent::GetReference ()
{
    if (_ppSteepestDescent == NULL) throw gcnew NativeNullException ();
	return **_ppSteepestDescent;
}
boost::shared_ptr<QuantLib::SteepestDescent>& Cephei::QL::Math::Optimization::CSteepestDescent::GetShared ()
{
    if (_ppSteepestDescent == NULL) throw gcnew NativeNullException ();
	return *_ppSteepestDescent;
}
QuantLib::SteepestDescent* Cephei::QL::Math::Optimization::CSteepestDescent::GetPointer ()
{
    if (_ppSteepestDescent == NULL) throw gcnew NativeNullException ();
	return &**_ppSteepestDescent;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SteepestDescent>& Cephei::QL::Math::Optimization::CSteepestDescent::GetHandle ()
{
	if (_phSteepestDescent == NULL)
	{
		_phSteepestDescent = new Handle<QuantLib::SteepestDescent> (*_ppSteepestDescent);
	}
	return *_phSteepestDescent;
}
#endif
bool Cephei::QL::Math::Optimization::CSteepestDescent::HasNative () 
{
	return (_ppSteepestDescent != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Optimization::ISteepestDescent^ Cephei::QL::Math::Optimization::CSteepestDescent_Factory::Create (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::Optimization::ILineSearch^>^ lineSearch)
{
    return gcnew CSteepestDescent ( lineSearch);
}
